package com.by.service.impl;

import com.by.bean.User;
import com.by.bean.UserType;
import com.by.common.bean.PageBean;
import com.by.common.ex.BusinessException;
import com.by.common.utils.Assert;
import com.by.mapper.UserMapper;
import com.by.query.EmpQuery;
import com.by.service.EmpService;
import com.by.bean.Emp;
import com.by.mapper.EmpMapper;
import com.by.common.crud.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class EmpServiveImpl extends BaseServiceImpl<Emp,EmpMapper> implements EmpService {

    @Autowired
    EmpMapper empMapper;

@Autowired
private UserMapper userMapper;

    @Transactional
    @Override
    public int save(Emp emp) {
        //检查用户是否存在
        int c=userMapper.queryUserByUserName(emp.getAccount());
        if(c>0)
            throw new BusinessException("该账号已存在！");
        //先保存用户
        User user = new User(emp.getAccount(), emp.getPassword(), UserType.CUSTOMER_USER);
        int rs = userMapper.save(user);
        Assert.isZero(rs, "保存用户失败");
        //保存客户
        //设置客户id
        emp.setEmployee_id(user.getId());
        return super.save(emp);
    }
    @Override
    public PageBean query(EmpQuery query) {
        //分页查询
        PageHelper.startPage(query.getPage(), query.getPageSize());//后面紧跟查询
        List list = baseMapper.query(query);
        PageInfo info = new PageInfo(list);
        return new PageBean(query.getPage(), query.getPageSize(), info.getTotal(), list);
    }
    @Transactional
    @Override
    public int delete(Serializable id) {
        //删除用户
        int rs=userMapper.delete(id);
        Assert.isZero(rs,"删除用户失败");
        return super.delete(id);
    }


}
